//
//  YBSecViewController.m
//  OurTourism
//
//  Created by 孟镇 on 15/11/26.
//  Copyright © 2015年 Devil_Coming. All rights reserved.
//

#import "YBSecViewController.h"
#import "YBpbstoryflowlayout.h"
#import "YBSecCell.h"
#import "Header.h"
#import "YBStorydetialViewController.h"
//#import "YBStoryitemCollectionViewCell.h"
#import "YBDestinationModel.h"
static CGFloat const kMargin = 10.f;
static NSString * const reuseIdentifier = @"YBSecCell";
@interface YBSecViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) NSMutableArray *dataList;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) YBpbstoryflowlayout *layout;
@property (nonatomic, retain)MBProgressHUD *hud;
@property (nonatomic, retain)NSMutableArray *modelDataArr; //总数据数组
@property (nonatomic, retain)NSMutableArray *modelUserDataArr; //用户信息数组
@property (nonatomic, assign)NSInteger startID; //开始ID

@end

@implementation YBSecViewController
#pragma mark init methods


- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.translucent = YES;
    self.automaticallyAdjustsScrollViewInsets = YES;
    //self.collectionView.frame = CGRectMake(0, 0, WIDTH, HEIGTH);
    
}




- (UICollectionView *)collectionView {
    if (!_collectionView) {
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, WIDTH, HEIGTH) collectionViewLayout:self.layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        [_collectionView registerClass:[YBSecCell class] forCellWithReuseIdentifier:reuseIdentifier];
    }
    return _collectionView;
}

- (YBpbstoryflowlayout *)layout {
    if (!_layout) {
        _layout = [[YBpbstoryflowlayout alloc] init];
        _layout.minimumInteritemSpacing = kMargin;
        _layout.minimumLineSpacing = kMargin;
        _layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    }
    return _layout;
}


#pragma mark - 获取数据
- (void)getData
{
    NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/spot/hot/list/?start=0"];
    
    [MZNetHandle getUrl:str cookie:nil result:^(id result) {
        
        NSMutableDictionary *dic = result;
        
        
        self.modelDataArr = [YBDestinationModel transformWithArray:dic[@"data"][@"hot_spot_list"]];
        
        NSMutableArray *userDataArr = [NSMutableArray array];
        for (NSDictionary *tempDic in dic[@"data"][@"hot_spot_list"]) {
            [userDataArr addObject:tempDic[@"user"]];
        }
        
        self.modelUserDataArr = [YBDestinationModel transformWithArray:userDataArr];
        
        
        [self.hud removeFromSuperview];
        
        [self creatView];
        
        
    }];
    
}





#pragma mark lifecycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithRed:250 / 255. green:246 / 255. blue:232 / 255. alpha:1];
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 25, 25);
    [backBtn setImage:[UIImage imageNamed:@"iconfont-iconfonticonfontjiantouzuo11@2x"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backItem;
    
    // Do any additional setup after loading the view, typically from a nib.
    [self loadMBProgress];
    [self getData];
}
- (void)loadMBProgress
{
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    
    self.hud.dimBackground = NO;
    //设置对话框文字
    self.hud.labelText = @"loading";
    self.hud.minShowTime = 50;
    self.hud.activityIndicatorColor = [UIColor whiteColor];
    self.hud.labelColor = [UIColor whiteColor];
    
    [self.view addSubview:self.hud];
    [self.hud showAnimated:YES whileExecutingBlock:^{
        //对话框显示时需要执行的操作
        sleep(1);
    } completionBlock:^{
        //操作执行完后取消对话框
        [self.hud removeFromSuperview];
        self.hud = nil;
    }];
    
}
- (void)MJRefresh
{
    self.startID = 12;
    self.collectionView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        
        NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com/v2/new_trip/spot/hot/list/?start=%ld", self.startID];
        [MZNetHandle getUrl:str cookie:nil result:^(id result) {
            NSMutableDictionary *dic = result;
            
            self.startID += 12;
            
            [self.modelDataArr addObjectsFromArray: [YBDestinationModel transformWithArray:dic[@"data"][@"hot_spot_list"]]];
            
            
            NSMutableArray *userDataArr = [NSMutableArray array];
            for (NSDictionary *tempDic in dic[@"data"][@"hot_spot_list"]) {
                [userDataArr addObject:tempDic[@"user"]];
            }
            
            [self.modelUserDataArr addObjectsFromArray: [YBDestinationModel transformWithArray:userDataArr]];
            
            
            [self.collectionView reloadData];
            [self.collectionView.mj_footer endRefreshing];
            
            
        }];
    }];
}

- (void)creatView
{
    
    self.dataList = [NSMutableArray array];
    int a = 150;
    CGFloat width = ([UIScreen mainScreen].bounds.size.width - 2*kMargin)/2.f;
    for (NSUInteger idx = 0; idx < 100; idx ++) {
        if (idx  == 1) {
            a = 150;
        }else
        {
            a = 200;
        }
        //  CGFloat height = 150 + (arc4random() % 60);
        NSValue *value = [NSValue valueWithCGSize:CGSizeMake(width,a)];
        [_dataList addObject:value];
    }
    
    [self.view addSubview:self.collectionView];
    [self MJRefresh];
}


#pragma mark UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.modelDataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    YBSecCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.storyAllModel = self.modelDataArr[indexPath.item];
    
    cell.layer.transform = CATransform3DMakeScale(0.8, 0.8, 0.1);
    [UIView animateWithDuration:0.5 animations:^{
        cell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
    }];
    if (indexPath.item == 1) {
        
        cell.titleLable.alpha = 0.7;
        return  cell;
    }else
    {
        return cell;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGSize size = [[_dataList objectAtIndex:indexPath.row] CGSizeValue];
    
    return  size;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
 
    YBStorydetialViewController *storyDetail = [[YBStorydetialViewController alloc]init];
    storyDetail.spot_id = ((YBDestinationModel *)self.modelDataArr[indexPath.item]).spot_id;
    storyDetail.hidesBottomBarWhenPushed = YES;
    //收藏
    storyDetail.recommendModel = self.modelDataArr[indexPath.item];
    storyDetail.userModel = self.modelUserDataArr[indexPath.item];
  
    [self presentViewController:storyDetail animated:YES completion:^{
    }];
}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
